package com.tools.cleanmaster.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.model.item.FileTrashItem;
import com.tools.cleanmaster.model.item.TrashItem;
import com.tools.cleanmaster.model.item.VideoTrashItem;
import com.tools.cleanmaster.utils.DateTimeUtils;
import com.tools.cleanmaster.utils.StringUtils;

import java.io.File;
import java.util.List;

/**
 * Created by lianzhan on 15/12/25.
 */
public class VideoLargeFileAdapter extends DeepAdapter {
    private List<TrashItem> mDatas;
    private int mType;

    public VideoLargeFileAdapter(Context context, List<TrashItem> datas, int type) {
        super(context);
        mDatas = datas;
        mType = type;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.large_file_item, null);

            holder = new Holder();
            holder.mImageView = convertView.findViewById(R.id.trash_result_file_icon);
            holder.mTitle = convertView.findViewById(R.id.trash_result_file_name);
            holder.mDurationTime = convertView.findViewById(R.id.trash_result_file_updatetime);
            holder.mSize = convertView.findViewById(R.id.trash_result_file_size);
            holder.mCheckBox = convertView.findViewById(R.id.trash_result_file_isdelete);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final TrashItem item = mDatas.get(position);
        if (item instanceof VideoTrashItem) {
            Glide.with(mContext).load(new File(item.filePath)).centerCrop().into(holder.mImageView);

            holder.mDurationTime.setText(((VideoTrashItem) item).mDuration);
            holder.mTitle.setText(((VideoTrashItem) item).mVideoName);
        } else if (item instanceof FileTrashItem) {
            holder.mTitle.setText(((FileTrashItem) item).mFileName);
            holder.mDurationTime.setText(DateTimeUtils.formatDate(((FileTrashItem) item).lastModified));
        }

        holder.mCheckBox.setChecked(item.isSelected);
        holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.isSelected = !item.isSelected;
                if (mDeleteOnclickCallback != null) {
                    mDeleteOnclickCallback.onClick(position, item);
                }
            }
        });
        holder.mSize.setText(StringUtils.getSizeString(item.size));
        return convertView;
    }

    @Override
    protected int getDefaultImageId() {
        return R.drawable.video_default;
    }

    public static class Holder {
        protected ImageView mImageView;
        protected TextView mTitle;
        protected TextView mDurationTime;
        protected TextView mSize;
        protected CheckBox mCheckBox;
    }
}
